package com.itheima.reggie.controller;

import cn.hutool.db.Session;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/1 11:32
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //员工登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Employee loginEmployee){//1.接收请求参数
        //2.调用service登录
       ResultInfo resultInfo =employeeService.login(loginEmployee.getUsername(),loginEmployee.getPassword());
       //3.返回结果
        return resultInfo;
    }

    @Autowired
    private HttpSession session;
    //员工退出
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        //1.自毁session
        session.invalidate();
        //2.返回成功
        return ResultInfo.success();
    }

    //查询员工列表
    @GetMapping("/employee/find")
    public ResultInfo find(String name){//1.接受参数
        //2.调用service
       List<Employee> list = employeeService.find(name);
       //3.返回结果
       return ResultInfo.success(list);
    }

    //新增员工
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        return ResultInfo.success();
    }
    //回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Employee employee=employeeService.findById(id);
        return ResultInfo.success(employee);
    }
    //修改员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success();
    }


}

